"""
使用ViewSet来演示自定义视图方法映射
"""

from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from .models import *
from .serializers import *


# 需求：修改书本的阅读量,部分更新
# PATCH + /books/<pk>/ = self.read
class BookSetView(ViewSet):

    # 自定义read函数来实现修改阅读量
    def read(self, request, pk):
        book = BookInfo.objects.get(pk=pk)
        bs = BookInfoModelSerializer(instance=book, data=request.data, partial=True)
        if not bs.is_valid():
            return Response(bs.errors, status=400)
        bs.save()
        return Response(bs.data)
